using System;
using MonoTouch.ObjCRuntime;
using MonoTouch.Foundation;

namespace Ro.Norc.ServiceStackClient
{
	/// <summary>
	/// This class groups some general purpose utility methods
	/// </summary>
	public static class ServiceUtil
	{
		/// <summary>
		/// Selector for the objc errorWithDomain:code:userInfo: method
		/// </summary>
		private static Selector _selInitError = new Selector("errorWithDomain:code:userInfo:");
		
		/// <summary>
		/// Create a new <see cref="Ro.Norc.ServiceStackClient.ServiceResultOneWay"/> instance and returns it as a System.Object
		/// </summary>
		/// <param name="responseCode">
		/// The HTTP response code
		/// </param>
		/// <param name="contentType">
		/// The content type
		/// </param>
		/// <returns>
		/// The newly created response object
		/// </returns>
		public static object GetResultOneWay(int responseCode, string contentType) 
		{
			return new ServiceResultOneWay() {
				ResponseCode = responseCode,
				ContentType = contentType
			};
		}
		
		/// <summary>
		/// Creates a NSError object from the given error code
		/// </summary>
		/// <remarks>
		/// This method actually wraps a calls to lower level methods which allow communication with the objc runtime, because the objc errorWithDomain:code:userInfo: method is not yet bound by MonoTouch
		/// </remarks>
		/// <param name="code">
		/// The error code
		/// </param>
		/// <returns>
		/// The resulting NSError
		/// </returns>
		public static NSError GetError(int code)
		{
			Class _nsErrorClass = new Class(typeof(NSError));
			IntPtr _nsErrorHandle = IntPtr.Zero;
			NSString _domain = new NSString(ServiceErrorDomain.ERROR_DOMAIN_CLIENT);
			
			_nsErrorHandle = Messaging.IntPtr_objc_msgSend_IntPtr_int_IntPtr(_nsErrorClass.Handle, _selInitError.Handle, _domain.Handle, code, IntPtr.Zero);
			
			return (NSError)Runtime.GetNSObject(_nsErrorHandle);
		}
	}
}